<?php

namespace backend\modules\person\controllers;

use Yii;
use common\components\Curd;
use common\models\base\SearchModel;
use common\models\person\Person;
use common\models\person\Cla;
use backend\modules\person\forms\ClaForm;
use backend\controllers\BaseController;

/**
 * Class NotifyAnnounceController
 * @package backend\modules\sys\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class ClaController extends BaseController
{
    use Curd;

    /**
     * @var person
     */
    public $modelClass = Cla::class;

    /**
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['class_name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
//            ->andWhere(['>=', 'status', StatusEnum::DISABLED])
//            ->andWhere(['type' => Notify::TYPE_ANNOUNCE])
                ;

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', null);
        $model = $this->findModel($id);
        if ($model->load($request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,
        ]);
    }

    /**
     * 返回模型
     *
     * @param $id
     * @return \yii\db\ActiveRecord
     */
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = Cla::findOne($id)))) {//这是增加
            $model = new ClaForm();
            return $model ->loadDefaultValues();
        }
        return $model;
    }
}